#include <Windows.h>
#include <Wininet.h>
#include <tchar.h>
#include <errno.h>
#include "URLDownloadTools.h"
#include "lib.h"
#include "dir.h"
#include "bfish.h"
#include "pathname.h"
#include "stream.h"
#include <zlib.h>
#include "zipfile.h"

static const String MANIFEST_SECTION = _T("manifest");
static const String INSTALLED = _T("installed");
static const String PACKAGELIST_INI = PACKAGELIST_INI_MACRO;
static const String URL_SEP = _T("/");
static const String _INI = _T(".ini");
static const String _ZIP = _T(".zip");
static const String _CRC = _T(".crc");

URLDownloadTools::DownloadError  
URLDownloadTools::DownloadPackage(LPCTSTR szBaseURL, 
                                  LPCTSTR szURL, 
                                  LPCTSTR szFileName, 
                                  uint32 crcVerify /* = 0 */, 
                                  const URLDownloadProgressEventHandler* pProgressHandler /* = NULL */, 
                                  bool bAutoUnZip /* = true */)
{
    // TODO: Finish this!!!!
    return DWNLDERR_UNEXPECTED;
}

URLDownloadTools::DownloadError 
URLDownloadTools::DownloadFile(LPCTSTR szBaseURL, 
                               LPCTSTR szURL, 
                               LPCTSTR szFileName, 
                               uint32 crcVerify /* = 0 */, 
                               const URLDownloadProgressEventHandler* pProgressHandler /* = NULL */, 
                               bool bAutoUnZip /* = true */)
{
    // TODO: Finish this!!!!
    return DWNLDERR_UNEXPECTED;
}

bool URLDownloadTools::ComputeFileCRC(LPCTSTR szFileName, uint32& crc32)
{
    // TODO: Finish this!!!!
    crc32 = 0;
    return false;
}

bool URLDownloadTools::IsPackageListIniUpToDate(const String& strPackageListINI, 
                                                bool bTraceAllErrors /* = false */)
{
    // TODO: Finish this!!!!
    return false;
}

int32 URLDownloadTools::GetPackageSectionNumber(const String& strPackageListINI, 
                                                const String& strPackageName, 
                                                bool bTraceAllErrors /* = false */)
{
    // TODO: Finish this!!!!
    return 0;
}

String URLDownloadTools::GetPackageVersion(const String& strPackageListINI, 
                                           const String& strPackageName, 
                                           bool bTraceAllErrors /* = false */)
{
    // TODO: Finish this!!!!
    return String::emptyString;
}

String URLDownloadTools::GetRemoteFolderName(const String& strPackageListINI, 
                                             const String& strPackageName, 
                                             bool bTraceAllErrors /* = false */)
{
    // TODO: Finish this!!!!
    return String::emptyString;
}

uint32 URLDownloadTools::GetExpectedPackageSize(const String& strPackageListINI, 
                                                const String& strPackageName, 
                                                bool bGetCompressedSize /* = true */,
                                                bool bTraceAllErrors /* = false */)
{
    // TODO: Finish this!!!!
    return 0;
}

uint32 URLDownloadTools::GetExpectedPackageCRC(const String& strPackageListINI, 
                                               const String& strPackageName, 
                                               bool bTraceAllErrors /* = false */)
{
    // TODO: Finish this!!!!
    return 0;
}

URLDownloadTools::DownloadError
URLDownloadTools::VerifyPackageContents(const String& strPackageName, 
                                        const String& strPackagePath, 
                                        bool bTraceErrorsAndProgress /* = false */)
{
    // TODO: Finish this!!!!
    return DWNLDERR_UNEXPECTED;
}

URLDownloadTools::DownloadError
URLDownloadTools::UnZipPackage(LPCTSTR szZipFileName, LPCTSTR szUnZipPath)
{
    // TODO: Finish this
    return DWNLDERR_WRITE_FAILED;
}

URLDownloadTools::DownloadError
URLDownloadTools::UnZipFile(LPCTSTR szZipFileName, LPCTSTR szUnZipPath)
{
    // TODO: Finish this
    return DWNLDERR_WRITE_FAILED;
}
